# Replication code for "International Clientelistic Networks and 
# International Fora: The Case of Venezuela at the United Nations 
# General Assembly (1999-2015)"

## Loading packages

library(dplyr)
library(tidyverse)
library(ggplot2)
library(stargazer)
library(here)
library(plm)
library(sandwich)
library(lmtest)
library(VennDiagram)
library(ggfortify)
library(reshape2)
library(gridExtra)
library(ggeffects)

## Loading database and creating the panel data frame

df <- read.csv("international_clientelism_data.csv")
pdf <- pdata.frame(df, index=c("CountryName", "year")) %>% rename(oil_price = oilp)

# Figure 2 - ALBA and PetroCaribe membership

ALBA <- c("Antigua and Barbuda", "Bolivia", "Cuba", "Bolivia", "Dominica", "Ecuador",
          "Grenada", "Honduras", "Nicaragua", "Saint Lucia", "Saint Kitts and Nevis",
          "Saint Vincent and the Grenadines")
PETR <- c("Antigua and Barbuda", "Belize", "Cuba", "Dominica", 
          "Dominican Republic", "El Salvador", "Grenada", "Guyana", "Haiti", "Honduras", 
          "Jamaica", "Nicaragua", "Saint Kitts and Nevis",
          "Saint Vincent and the Grenadines", "Suriname")

vdd <- venn.diagram(list(ALBA=ALBA, PetroCaribe=PETR),
                    filename=NULL)
vdd[[5]]$label <- paste(setdiff(PETR, ALBA), collapse="\n")
vdd[[6]]$label <- paste(setdiff(ALBA, PETR), collapse="\n")
vdd[[7]]$label <- paste(intersect(ALBA, PETR), collapse="\n")
grid.newpage()
grid.draw(vdd)
rm(vdd, ALBA, PETR)

## Figure 3 - Latin American and Caribbean countries’ voting agreement with Venezuela between 1999 and 2015

df$bol <- "No membership and non-left-wing"
df$bol[df$ALBA == 1 & df$PETROCARIBE ==0 & df$polori == 0] <- "ALBA only"
df$bol[df$PETROCARIBE == 1 & df$ALBA==0 & df$polori == 0] <- "PetroCaribe only"
df$bol[df$PETROCARIBE == 0 & df$ALBA==0 & df$polori == 1] <- "Left-wing only"
df$bol[df$ALBA == 1 & df$PETROCARIBE == 1 & df$polori==0] <- "PetroCaribe + ALBA or Left-Wing"
df$bol[df$ALBA == 0 & df$PETROCARIBE == 1 & df$polori==1] <- "PetroCaribe + ALBA or Left-Wing"
df$bol[df$ALBA == 1 & df$PETROCARIBE == 0 & df$polori==1] <- "ALBA + Left-Wing"
df$bol[df$ALBA == 1 & df$PETROCARIBE == 1 & df$polori==1] <- "ALBA + PetroCaribe + Left-Wing"

dfag <- aggregate(vzag ~ bol + year, mean, data=df)
dfag %>%
  ggplot(aes(year, vzag, linetype=bol, shape=bol, color=bol)) +
  geom_point() + 
  geom_line() +
  labs(y="Voting agreement with Venezuela", x="Year", 
       color="ALBA/PetroCaribe membership
       and political orientation", linetype="ALBA/PetroCaribe membership
       and political orientation", shape="ALBA/PetroCaribe membership
       and political orientation") +
  theme(panel.background = element_rect(fill='white', colour='black'), 
        legend.position = "bottom")
rm(dfag)

# Regression Analysis

## Hausman tests, based on one model

fixed3 <- plm(vzag ~ PETROCARIBE + ALBA + polori+
                logusimport + logusexport +
                logusaid + dem + loggdp,
               model= "within",
               data=pdf)

random3 <- plm(vzag ~ PETROCARIBE + ALBA + polori+
                logusimport + logusexport +
                logusaid + dem + loggdp,
              model= "random",
              data=pdf)
phtest(fixed3, random3)
rm(fixed3, random3)

## Estimating models

### Table 1 – Effects of Venezuelan clientelistic links on voting agreement with Venezuela

reg0a <- plm(vzag ~ PETROCARIBE, 
             effect="twoway",
             model="within", data=pdf)
se0a <- sqrt(diag(vcovHC(reg0a, type = "HC3", cluster="group")))
reg1a <- plm(vzag ~ PETROCARIBE + polori + ALBA, 
             effect="twoway",
             model="within", data=pdf)
se1a <- sqrt(diag(vcovHC(reg1a, type = "HC3", cluster="group")))
reg2a <- plm(vzag ~ PETROCARIBE + logusimport +
                logusexport + logusaid + dem + loggdp, 
             effect="twoway",
             model="within", data=pdf)
se2a <- sqrt(diag(vcovHC(reg2a, type = "HC3", cluster="group")))
reg3a <- plm(vzag ~ PETROCARIBE + ALBA + polori+
               logusimport + logusexport +
               logusaid + dem + loggdp, effect="twoway",
             model="within", data=pdf)
se3a <- sqrt(diag(vcovHC(reg3a, type = "HC3", cluster="group")))
reg4a <- plm(vzag ~ PETROCARIBE*ALBA + polori+
               logusimport + logusexport +
               logusaid + dem + loggdp, effect="twoway",
             model="within", data=pdf)
se4a <- sqrt(diag(vcovHC(reg4a, type = "HC3", cluster="group")))
reg5a <- plm(vzag ~ PETROCARIBE + ALBA + PETROCARIBE*polori+
               logusimport + logusexport +
               logusaid + dem + loggdp, effect="twoway",
             model="within", data=pdf)
se5a <- sqrt(diag(vcovHC(reg5a, type = "HC3", cluster="group")))
reg6a <- plm(vzag ~ PETROCARIBE*oil_price + ALBA + polori+
               logusimport + logusexport +
               logusaid + dem + loggdp, effect="individual",
             model="within", data=pdf)
se6a <- sqrt(diag(vcovHC(reg6a, type = "HC3", cluster="group")))

stargazer(reg0a, reg1a, reg2a, reg3a, reg4a, reg5a, reg6a, type = "html", out="Table1.html",
          se=list(se0a, se1a, se2a, se3a, se4a, se5a, se6a),
          dep.var.labels = "Voting Agreement with Venezuela",
          notes.append = FALSE, notes.align = "l", omit.stat=c("ser"),
          notes = "Robust standard errors are in parentheses, clustered by country. Models 1 to 6 include country and year fixed-effects. Model 7 include only country-fixed effects, because oil prices is a unit-invariant variable. *p<0.1; **p<0.05; ***p<0.01")
rm(reg0a, reg1a, reg2a, reg3a, reg4a, reg5a, reg6a,
   se0a, se1a, se2a, se3a, se4a, se5a, se6a)


### Table 2 - Effects of vulnerability on adhering to PetroCaribe and ALBA

car <- c("Antigua & Barbuda", "Bahamas", "Barbados", "Belize", "Costa Rica", "Cuba",
         "Dominca", "Dominican Republic", "El Salvador", "Grenada", "Guatemala",
         "Guyana", "Haiti", "Honduras", "Jamaica", "Nicaragua", "Panama",
         "St. Kitts and Nevis", "St. Lucia", "St. Vincent and the Grenadines",
         "Suriname", "Trinidad and Tobago")
df$logtpop <- log(df$population)
pdd <- df %>% filter(year >2004)
pdd2 <- df %>% filter(year >2004) %>% filter(CountryName %in% car)

vbp <- glm(PETROCARIBE ~ loggdp + oilrent + logtpop, 
           data=pdd, family=binomial)
vbp2 <- glm(PETROCARIBE ~ loggdp + oilrent + logtpop, 
           data=pdd2, family=binomial)
vba<- glm(ALBA ~ loggdp + oilrent + logtpop, 
          data=pdd, family=binomial)
vba2<- glm(ALBA ~ loggdp + oilrent + logtpop, 
          data=pdd2, family=binomial)
stargazer(vbp, vbp2, vba, vba2, type = "html", out="Tablevb.html",
          dep.var.labels = c("PetroCaribe", "ALBA"),
          notes.append = FALSE, notes.align = "l", omit.stat=c("ser"),
          add.lines=list(c("Sample", "Full", "Central America and the Caribbean", "Full", "Central America and the Caribbean")),
          notes = "*p<0.1; **p<0.05; ***p<0.01")

inte <- glm(PETROCARIBE ~ loggdp*polori, 
           data=pdd, family=binomial)
inte2<- glm(ALBA ~ loggdp*polori, 
          data=pdd, family=binomial)
pd1 <- ggpredict(inte, c("loggdp","polori")) %>% mutate(group = ifelse(group==0, "Non Left-Wing", "Left-Wing"))
pd1 <- plot(pd1, colors="bw") + labs(linetype = "Political Orientation")
pd2 <- ggpredict(inte2, c("loggdp","polori")) %>% mutate(group = ifelse(group==0, "Non Left-Wing", "Left-Wing"))
pd2 <- plot(pd2, colors="bw") + labs(linetype = "Political Orientation")
grid.arrange(pd1, pd2, ncol=1)
rm(car, pdd, pdd2, vbp, vbp2, vba, vba2, inte, inte2, pd1, pd2)

### Table 3 – Effects of Venezuelan clientelistic links over Latin American and Caribbean countries’ voting agreement with Venezuela at the UNGA by thematic area 

HR <- plm(HR~ PETROCARIBE + ALBA + polori+
            logusimport + logusexport +
            logusaid + dem + loggdp, effect="twoway",
          model="within", data=pdf)
seHR <- sqrt(diag(vcovHC(HR, type = "HC3", cluster="group")))

ED <- plm(ED ~ PETROCARIBE + ALBA + polori+
            logusimport + logusexport +
            logusaid + dem + loggdp, effect="twoway",
          model="within", data=pdf)
seED <- sqrt(diag(vcovHC(ED, type = "HC3", cluster="group")))
summary(ED)
CO <- plm(CO ~ PETROCARIBE + ALBA + polori+
            logusimport + logusexport +
            logusaid + dem + loggdp, effect="twoway",
          model="within", data=pdf)
seCO <- sqrt(diag(vcovHC(CO, type = "HC3", cluster="group")))

PC <- plm(PC ~ PETROCARIBE + ALBA + polori+
            logusimport + logusexport +
            logusaid + dem + loggdp, effect="twoway",
          model="within", data=pdf)
sePC <- sqrt(diag(vcovHC(PC, type = "HC3", cluster="group")))

NU <- plm(NU ~ PETROCARIBE + ALBA + polori+
            logusimport + logusexport +
            logusaid + dem + loggdp, effect="twoway",
          model="within", data=pdf)
seNU <- sqrt(diag(vcovHC(NU, type = "HC3", cluster="group")))

DI <- plm(DI ~ PETROCARIBE + ALBA + polori+
            logusimport + logusexport +
            logusaid + dem + loggdp, effect="twoway",
          model="within", data=pdf)
seDI <- sqrt(diag(vcovHC(DI, type = "HC3", cluster="group")))

imp <- plm(impvt ~ PETROCARIBE + ALBA + polori+
             logusimport + logusexport +
             logusaid + dem + loggdp, effect="twoway",
           model="within", data=pdf)
seimp <- sqrt(diag(vcovHC(imp, type = "HC3", cluster="group")))

stargazer(HR, ED, CO, PC, NU, DI, imp, type = "html", out="Table2.html",
          dep.var.labels = c("Human Rights", 
                             "Economic Development", "Colonialism",
                             "Palestinian Conflict", "Nuclear Matters",
                             "Arms control/International Disarmament",
                             "Important resolutions for the U.S."),
          se=list(seHR, seED, seCO, sePC, seNU, seDI, seimp),
          notes.append = FALSE, notes.align = "l",
          notes = "Robust standard errors are in parentheses, clustered by country. All models include country and year fixed-effects. *p<0.1; **p<0.05; ***p<0.01")
rm(HR, ED, CO, PC, NU, DI,imp,
   seHR, seED, seCO, sePC, seNU, seDI, seimp)

